package cn.blog.controller;

import cn.blog.entity.Blog;
import cn.blog.entity.PageBean;
import cn.blog.service.BlogService;
import cn.blog.util.StringUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author 林泽豪
 * @create 2019-10-29 12:05
 */
@Controller
@RequestMapping("/blog")
public class NewBlogController {
    @Resource
    BlogService blogService;

    @RequestMapping("/list")
    public String blogList(@RequestParam(value = "page",required = false)String page,
                           @RequestParam(value = "rows",required = false)String rows,
                           HttpServletRequest request, HttpServletResponse response, Blog blog) {
        PageBean pageBean = new PageBean(Integer.parseInt(page), Integer.parseInt(rows));
        Map<String, Object> map = new HashMap<>();
        map.put("start",pageBean.getPageSize());
        map.put("size",pageBean.getPageSize());
        map.put("title", StringUtil.formatLike(blog.getTitle()));
        List<Blog> blogList = blogService.list(map);
        Long total = blogService.getTotal(map);
        request.setAttribute("blogList", blogList);
        request.setAttribute("total", total);
        return "/admin/blogManage";
    }
}
